package vip.xiaonuo.hontu.issued.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import vip.xiaonuo.hontu.issued.constant.HTConstant;

import java.util.Objects;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.iservice.IMemberDealWithService;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.rule.HTRulesEngine;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;

@Service(value =RuleConstant.HT + RuleConstant.MEMBER_DEAL_WITH)
public class HTMemberDealWithServiceImpl implements IMemberDealWithService {


    @Autowired
    private MemberDeviceService memberDeviceService;

    @Autowired
    private DeviceLogService deviceLogService;

    @Autowired
    private MemberUserService memberUserService;

    @Autowired
    private DwDeviceSetService dwDeviceSetService;


    @Override
    public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
        return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
    }

    @Override
    public void deleteException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        memberDevice.setFailReason(ExceptionUtils.getMessage(e));
        memberDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, DELETE);
    }

    @Override
    public void delete(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        deletePersonRequest.setIp(deviceBase.getCabinetIp());
        deletePersonRequest.setPort(deviceBase.getCabinetPort());
        deletePersonRequest.setPersonnelId("1" + memberUser.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.deletePerson(null, deletePersonRequest);
        memberDeviceService.removeById(memberDevice.getId());
        long count = memberDeviceService.count(new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getMemberId, memberUser.getId()));
        if (count == 0 && Objects.equals(memberUser.getDelFlag(), "1")) {
            memberUserService.removeById(memberDevice.getMemberId());
        }
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, DELETE);
    }


    @Override
    public void add(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet personBaseParamRet, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }
        AddPersonRequest addPersonRequest = new AddPersonRequest();
        addPersonRequest.setIp(deviceBase.getCabinetIp());
        addPersonRequest.setPort(deviceBase.getCabinetPort());
        addPersonRequest.setPersonType(memberUser.getPersonType());
        addPersonRequest.setName(memberUser.getName());
        addPersonRequest.setFaceFeatureImgPath(memberUser.getPhoto());
        addPersonRequest.setPersonnelId("1" + memberUser.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.addPerson(null, addPersonRequest);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);
        memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        memberDeviceService.updateById(memberDevice);
    }


    @Override
    public void addException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        // 直接修改为失败
        memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        memberDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
    }

    @Override
    public void updateException(MemberDevice memberDevice, MemberUser memberUser, DeviceBase deviceBase, Exception e) {
        // 直接修改为失败
        memberDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        memberDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void update(MemberDevice memberDevice, MemberUser memberUser, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }
        UpdatePersonRequest addPersonRequest = new UpdatePersonRequest();
        addPersonRequest.setIp(deviceBase.getCabinetIp());
        addPersonRequest.setPort(deviceBase.getCabinetPort());
        addPersonRequest.setPersonType(memberUser.getPersonType());
        addPersonRequest.setName(memberUser.getName());
        addPersonRequest.setFaceFeatureImgPath(memberUser.getPhoto());
        addPersonRequest.setPersonnelId("1" + memberUser.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.updatePerson(null, addPersonRequest);
        memberDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        memberDeviceService.updateById(memberDevice);
        deviceLogService.addMemberLog(memberUser.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);
    }
}
